package com.base.cn.platform.os.common;

import com.base.cn.platform.os.common.msg.MsgTopic;
import net.oschina.j2cache.autoconfigure.J2CacheSpringRedisAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * 文 件 名:RedisSubscriberConfig
 * 创 建 人:hackeridear
 * 创建日期:2018-12-19
 * 描   述:RedisSubscriberConfig
 * 修 改 人:
 * 修改日期:
 * 版 本 号:v 1.0.0
 */
@Configuration
@AutoConfigureAfter({ J2CacheSpringRedisAutoConfiguration.class })
public class RedisSubscriberConfig {

    /** 系统统计主题 **/
    private final static String STAT_CHANNEL = MsgTopic.statistics_channel.name();

    @Bean
    public RedisMessageListenerContainer container(@Autowired JedisConnectionFactory redisConnectionFactory, @Autowired MessageListenerAdapter listenerAdapter){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic(STAT_CHANNEL));
        return container;
    }

    @Bean
    public MessageListenerAdapter messageListenerAdapter(
            @Autowired StatMessageReceiver statMessageReceiver) {
        return new MessageListenerAdapter(statMessageReceiver,"getMsg");
    }



}
